﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.Composition.Hosting;
using Microsoft.Practices.ServiceLocation;
using System.ComponentModel.Composition;
using System.Windows;
using System.Windows.Media;

namespace Dynamic_UI_Framework
{
    public abstract class MefBootstrapper : Bootstrapper
    {
        public MefBootstrapper(Setting setting) : base(setting) { }

        protected AggregateCatalog AggregateCatalog { get; set; }

        protected CompositionContainer Container { get; set; }

        public override void Run(bool runWithDefaultConfiguration)
        {
            this.AggregateCatalog = this.CreateAggregateCatalog();
            this.ConfigureAggregateCatalog();

            this.Container = this.CreateContainer();
            if (this.Container == null)
            {
                throw new InvalidOperationException("NullCompositionContainerException");
            }

            this.ConfigureContainer();
            
            this.ConfigureServiceLocator();
            this.ConfigureRegionAdapterMappings();

            var shell = this.Container.GetExportedValue<Workbench>();
            shell.CurrentContent = new MButton { Background = Brushes.Red };
            shell.Show();


            //var shell = this.Container.GetExportedValue<Shell>();
            //shell.Show();

            ResourceDictionary defaultResource = new ResourceDictionary { Source = new Uri("Dynamic UI Framework;component/Items/DesignerItem/DesignerItemControl.xaml", UriKind.Relative) };
            Application.Current.Resources.MergedDictionaries.Add(defaultResource);
            ToolBar tb = new ToolBar();
            tb.Show();
        }

        protected override void ConfigureRegionAdapterMappings()
        {
            base.ConfigureRegionAdapterMappings();

            ElementController ec = ServiceLocator.Current.GetInstance<ElementController>();
            ec.Mapping();

            PartController pc = ServiceLocator.Current.GetInstance<PartController>();
            pc.Mapping();

        }

        protected virtual AggregateCatalog CreateAggregateCatalog()
        {
            return new AggregateCatalog();
        }

        protected virtual void ConfigureAggregateCatalog()
        {
            this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(MefBootstrapper).Assembly));
        }

        protected virtual CompositionContainer CreateContainer()
        {
            CompositionContainer container = new CompositionContainer(this.AggregateCatalog);
            return container;
        }

        protected virtual void ConfigureContainer()
        {
            this.Container.ComposeExportedValue<IServiceLocator>(new MefServiceLocator(this.Container));
            this.Container.ComposeExportedValue<AggregateCatalog>(this.AggregateCatalog);
        }

        protected override void ConfigureServiceLocator()
        {
            IServiceLocator serviceLocator = this.Container.GetExportedValue<IServiceLocator>();
            ServiceLocator.SetLocatorProvider(() => serviceLocator);
        }

        public override object GetRootBench()
        {
            UserControlBenchAdapter adapter = ServiceLocator.Current.GetInstance<UserControlBenchAdapter>();
            if (adapter == null)
                return null;

            return adapter.DefaultBuild("root");
        }
    }
}
